﻿using Castle.DynamicProxy;
using Net.Caching;
using Newtonsoft.Json;
using System.Reflection;

namespace Net.Core
{
    /// <summary>
    /// 面向切换的内存缓存使用
    /// </summary>
    public class MemoryCacheAop : CacheAopBase
    {
        //通过注入的方式，把缓存操作接口通过构造函数注入
        private readonly ICachingProvider _cache;
        public MemoryCacheAop(ICachingProvider cache)
        {
            _cache = cache;
        }

        //Intercept方法是拦截的关键所在，也是IInterceptor接口中的唯一定义
        public override void Intercept(IInvocation invocation)
        {
            var method = invocation.MethodInvocationTarget ?? invocation.Method;
            //对当前方法的特性验证
            //如果需要验证
            //var CachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute));
            var CachingAttribute = method.GetCustomAttribute<CachingAttribute>(true);
            Console.WriteLine(method.Name);
            Console.WriteLine(method.DeclaringType.FullName);

            if (CachingAttribute is CachingAttribute qCachingAttribute)
            {
                //获取自定义缓存键
                var cacheKey = CustomCacheKey(invocation);
                //根据key获取相应的缓存值
                var cacheValue = _cache.Get(cacheKey);               
                if (cacheValue != null)
                {
                    //将当前获取到的缓存值，赋值给当前执行方法
                    invocation.ReturnValue = cacheValue;
                    Console.WriteLine( DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 获取缓存中的数据，"+ "缓存key：" + cacheKey );
                    return;
                }               
                //去执行当前的方法
                invocation.Proceed();
                //存入缓存
                if (!string.IsNullOrWhiteSpace(cacheKey))
                {
                    _cache.Set(cacheKey, invocation.ReturnValue, qCachingAttribute.AbsoluteExpiration);
                    var result = new { cacheKey, invocation.ReturnValue };
                    Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 成功写入缓存数据，" + "缓存key：" + cacheKey );
                }
            }
            else
            {
                invocation.Proceed();//直接执行被拦截方法
            }
        }

    }
}
